from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.expected_conditions import alert_is_present
from selenium.webdriver.support.wait import WebDriverWait
from common.Utils import BlogDriver

#博客登录页面
class BlogLogin:
    url = ""
    driver = ""
    def __init__(self):
        self.url="http://8.137.19.140:9090/blog_login.html"
        self.driver=BlogDriver.driver
        self.driver.get(self.url)
    #成功登录的测试用例
    def LoginSucTest(self):
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("zhangsan")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        #找到客户首页用户的昵称，说明登录成功，否则则失败
        wait= WebDriverWait(self.driver,1)
        wait.until(EC.visibility_of_element_located((By.XPATH, "/html/body/div[2]/div[1]/div/h3")))
        user_name=self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > h3").text
        print(user_name)
        #返回到登录页面,还有清除输入框
        self.driver.back()
        # self.driver.quit()
    #异常登录的测试用例，记得测试完的正确登录不要退出
    def LoginFailTest(self):
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsan")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        #检查是否登录失败，因为登录失败是给弹窗，而且要点击弹窗，所以我们要用
        wait=WebDriverWait(self.driver,1)
        #wait.until(EC.visibility_of_element_located(alert_is_present()))
        wait.until(EC.alert_is_present())
        BlogDriver.getScreeShot()
        #然后点击弹窗
        #切换到弹窗
        #alert=self.driver.switch.alert
        alert = self.driver.switch_to.alert
        #确认
        alert.accept()
        #因为根本没进入到列表页，所以不需要回退
        #self.driver.back()

        #self.driver.quit()
        #错误账号和错误密码
    def LoginFail2Test(self):
        wait = WebDriverWait(self.driver, 1)
        #等待登录页面用户名输入框出现
        #这个presence_of_element_located没见过
        # wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "#username")))
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("wangwu")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        # 检查是否登录失败，因为登录失败是给弹窗，而且要点击弹窗，所以我们要用
        wait.until(EC.alert_is_present())
        BlogDriver.getScreeShot()
        alert = self.driver.switch_to.alert
        alert.accept()

        #self.driver.quit()
#错误账号正确密码
    def LoginFail3Test(self):
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("wangwu")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        wait = WebDriverWait(self.driver, 1)
        #点击弹窗
        wait.until(EC.alert_is_present())
        BlogDriver.getScreeShot()
        alert = self.driver.switch_to.alert
        alert.accept()
        # self.driver.quit()
#创建一个类对象要走main这里要注销，让这个文件老老实实作为一个类存在
# login = BlogLogin()
# login.LoginSucTest()
# login.LoginFailTest()
# login.LoginFail2Test()
# login.LoginFail3Test()






